package admin.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("*.do")
public class MyFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {


    }

    //处理页面乱码过滤器
    @Override
    //请求和相应时都会执行doFilter()方法
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest)servletRequest;
        //处理请求乱码
        request.setCharacterEncoding("utf-8");

        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //处理响应乱码
        response.setContentType("text/html;charset=utf-8");

        // 请求通过进入下个过滤器方法;Filter链式处理，请求依然转发到其他目的地址
        filterChain.doFilter(servletRequest,servletResponse);

    }

    @Override
    public void destroy() {


    }
}
